﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Diagnostics;
using System.ComponentModel;
using WPFExcelReport.Helpers;
using System.Windows.Input;
namespace WPFExcelReport
{
    public partial class TWTDF
    {
        public TWTDF()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWTEINZ();
            LMS = "D";
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if(dwMaster==null)
            {
                //DFS = "1";
            }
            else
            {
                DFS = Convert.ToInt16(dwMaster["VALUE"].ToString()).ToString();
            }
        }
        public override void OnINFOChg(string info)
        {
            if (INFO == "")
            {
                TBMain.Clear();
                return;
            }
            else
            {

            }
            base.OnINFOChg(info);
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
           foreach(DataRow row in TBMain.Rows)
            { 
                string pattern = "";
                int dfs = Convert.ToInt16(DFS);
                for (int idx = 0; idx < dfs; idx++)
                {
                    string pos = "";
                    pos = Helper.Right("0000" + idx, 4);
                    pattern += row[LMS + pos].ToString();  
                }
                row["TXTKTEXT"] = Helper.Left(pattern + "000000000000000000000000000000000000000000000000000000000000000000000000", 72);
                TMV.VTK.UpdateTXTKON(row);
            }            
            base.OnSave();
        }
        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            TBMain.Clear();
            if (ORDERNUM.Length < 15)
                return;
            dwData = TBMain.NewRow();
            dwData["WTEEINZNR"] = (app.Mandant + ORDERNUM.Trim()).PadRight(23) + "D";
            SResult = TMV.QWTDF(dwData);
            MergeData(SResult, true);

            if (SResult.Tables[0].Rows.Count == 0)
                return;
            int dfs = Convert.ToInt16(SResult.Tables[0].Rows[0]["VALUE"].ToString());
            LOADPATTERN(SResult,dfs);
            TBDF = null;
            TBDF = TBMain;  
        }

        public override void OnDFSChg(string _dfs)
        {
            try
            {
                base.OnDFSChg(_dfs);
                if (!IsEdit)
                    return;
                InitialTable(_dfs);
                TBDF = null;
                TBDF = TBMain;
                return;
                DataGridTemplateColumn cDFX = null;
                string cDFn = "cDF";
                int dfs = Convert.ToInt16(_dfs);
                for (int i = 1; i <= 16; i++)
                {
                    cDFn = "cDF" + Helper.Right("00" + i, 2);

                    cDFX = (DataGridTemplateColumn)this.FindName(cDFn);
                    if (cDFX == null)
                        continue;
                    cDFX.Visibility = System.Windows.Visibility.Hidden;
                    if (i < dfs)
                    {
                        cDFX.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {

                    }
                }
             
                int count = TBMain.Rows.Count;
                if (count == 0)
                    return;
                DataRow dwTemp = TBMain.Clone().NewRow();
                dwTemp.ItemArray = TBMain.Rows[0].ItemArray;
                dwTemp["LINE"] = 0;
                for (int idx = 0; idx < dfs; idx++)
                {
                    string pos = Helper.Right("0000" + idx, 4);
                    dwTemp[LMS + pos] = 0;
                }
                TBMain.Clear();
                TBMain.AcceptChanges();
                TMV.VTK.DTK02(dwTemp);
                for (int i = 1; i <= dfs; i++)
                {
                    dwData = TBMain.Rows.Add();
                    dwData.ItemArray = dwTemp.ItemArray;
                    dwData["LINE"] = i * 10;
                    dwData["TXTKPOS"] = dwData["LINE"];
                    TMV.VTK.BATCHWB14(dwData);
                }
                TMV.VTK.POSTCHG();
                TBDF = null;
                TBDF = TBMain;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public override void ByteChgCommand(object sender, MouseButtonEventArgs e)
        {
            if (!IsEdit)
                return;
            Image img = sender as Image;
            if (((DataGrid)master).CurrentItem == null)
                return;
            DataRow dwNow = ((DataRowView)((DataGrid)master).CurrentItem).Row;
            if (dwNow == null)
                return;

            dwNow[img.Tag.ToString()] = Math.Abs(Convert.ToInt16(dwNow[img.Tag.ToString()]) - 1);
            DFPS = "-1";
            DFPS = dwNow["LINE"].ToString() + "," + img.Tag.ToString() + "," + dwNow[img.Tag.ToString()].ToString();
            this.Focus();
        }
    }
}
